package com.zkingsoft.actions.app.interfaces;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.distribution.DistributionCategory;
import com.zkingsoft.model.distribution.DistributionImages;
import com.zkingsoft.model.distribution.DistributionProduct;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DistributionCategoryService;
import com.zkingsoft.services.distribution.DistributionImagesService;
import com.zkingsoft.services.distribution.DistributionProductService;
import com.zkingsoft.util.WebUtil;

@Controller
@RequestMapping(value = "app/goods")
public class AppGoodsController extends BaseController {

	@Resource
	private DistributionProductService goodService;
	
	@Resource
	private DistributionCategoryService categoryService;
	
	@Resource
	private DistributionImagesService imgService;
	
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(String queryText, PaginationVO pageVo, Long parentId, Long childId) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
//		pageVo.setLimit(0);
		List<DistributionProduct> dataList = goodService.findAppGoodList(queryText, pageVo, parentId, childId);
		for(DistributionProduct data : dataList) {
			DistributionImages query = new DistributionImages();
			query.setProId(data.getProId());
			query.setImgType((long) 1);
			
			PaginationVO queryPage = new PaginationVO();
			queryPage.setLimit(0);
			queryPage.setSort("img_sequence");
			queryPage.setOrder("asc");
			List<DistributionImages> imgs = imgService.findInPage(query, queryPage);
			data.setGoodImgList(imgs);
		}
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, dataList);
		return result;
	}
	
	/**
	 * 查看商品详情
	 */
	@RequestMapping(value = "/toDetail")
	public String toDetail(Long proId) {
		DistributionProduct good = goodService.findById(proId);
		
		DistributionImages query = new DistributionImages();
		query.setProId(good.getProId());
		
		PaginationVO queryPage = new PaginationVO();
		queryPage.setLimit(0);
		queryPage.setSort("img_sequence");
		queryPage.setOrder("asc");
		
		List<DistributionImages> imgs = imgService.findInPage(query, queryPage);
		good.setGoodImgList(imgs);
		
		WebUtil.setRequestAttribute("good", good);
		return "app/goods/goods_detail";
	}
	
	/**
	 * 查看商品分类
	 */
	@RequestMapping(value = "/toCategory")
	public String toCategory() {
		List<DistributionCategory> categoryParent = new ArrayList<DistributionCategory>();
		Map<Long, List<DistributionCategory>> categoryChild = new HashMap<Long, List<DistributionCategory>>();
		
		List<DistributionCategory> dataList = categoryService.findByModel(null);
		if(dataList != null && !dataList.isEmpty()) {
			for(DistributionCategory category : dataList) {
				if(category == null || category.getCatParentId() == 0) {
					categoryParent.add(category);
				} else {
					List<DistributionCategory> children = categoryChild.get(category.getCatParentId());
					if(children == null) {
						children = new ArrayList<DistributionCategory>();
					}
					children.add(category);
					categoryChild.put(category.getCatParentId(), children);
					
				}
			}
		}
		WebUtil.setRequestAttribute("categoryParent", categoryParent);
		WebUtil.setRequestAttribute("categoryChild", categoryChild);
		return "app/goods/category";
	}
	
	/**
	 * 选中分类
	 */
	@RequestMapping(value="chooseCategory")
	public String chooseCategory(Long parentId, Long childId) {
		
		WebUtil.setRequestAttribute("parentId", parentId);
		WebUtil.setRequestAttribute("childId", childId);
		return "app/goods/goods_center";
	}
	
}
